package com.zmj.sy.mom.srv.aps.service;

import cn.hutool.core.date.DatePattern;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmj.sy.mom.srv.aps.bean.entity.log.LogNestExce;
import com.zmj.sy.mom.srv.aps.bean.entity.mes.WmsPlatePick;
import com.zmj.sy.mom.srv.aps.bean.entity.third.SinoCamReceiveNestPart;
import com.zmj.sy.mom.srv.aps.bean.entity.third.SinoCamReceiveNestPlate;
import com.zmj.sy.mom.srv.aps.bean.vo.logNestExce.LogNestExceReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.logNestExce.LogNestExceResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.logNestExce.PickPlateInfoResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseIdReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseListResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BasePageResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.Result;
import com.zmj.sy.mom.srv.aps.bean.vo.nest.NestPlatePushDto;
import com.zmj.sy.mom.srv.aps.feign.ApsMesFeign;
import com.zmj.sy.mom.srv.aps.mapper.LogNestExceMapper;
import com.zmj.sy.mom.srv.aps.mapper.SinoCamReceiveNestPartMapper;
import com.zmj.sy.mom.srv.aps.mapper.SinoCamReceiveNestPlateMapper;
import com.zmj.sy.mom.srv.aps.mapper.WmsPlatePickMapper;
import com.zmj.sy.mom.srv.aps.utils.SyExceptionUtils;
import com.zmj.sy.mom.srv.aps.utils.SySecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class LogNestExceService {

    private final LogNestExceMapper logNestExceMapper;
    private final SinoCamReceiveNestPlateMapper sinoCamReceiveNestPlateMapper;
    private final ApsMesFeign apsMesFeign;
    private final SinoCamReceiveNestPartMapper sinoCamReceiveNestPartMapper;
    private final WmsPlatePickMapper wmsPlatePickMapper;

    public BasePageResVo<LogNestExceResVo> getExcePart(LogNestExceReqVo reqVo) {
        LambdaQueryWrapper<LogNestExce> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.hasText(reqVo.getAppId()), LogNestExce::getAppId, reqVo.getAppId());
        lqw.like(StringUtils.hasText(reqVo.getPlateCode()), LogNestExce::getPlateCode, reqVo.getPlateCode());
        lqw.like(StringUtils.hasText(reqVo.getConstructNo()), LogNestExce::getConstructNo, reqVo.getConstructNo());
        lqw.like(StringUtils.hasText(reqVo.getNestId()), LogNestExce::getNestId, reqVo.getNestId());
        lqw.like(StringUtils.hasText(reqVo.getPartId()), LogNestExce::getPartId, reqVo.getPartId());
        lqw.like(reqVo.getInfoState() != null, LogNestExce::getInfoState, reqVo.getInfoState());
        lqw.orderByAsc(LogNestExce::getInfoState);
        lqw.orderByDesc(LogNestExce::getCreateDateTime);
        Page<LogNestExce> page = logNestExceMapper.selectPage(reqVo.toPage(), lqw);
        return BasePageResVo.of(page, LogNestExceResVo.class);
    }

    public BaseListResVo<PickPlateInfoResVo> getPlateInfo(BaseIdReqVo reqVo) {
        LogNestExce logNestExce = logNestExceMapper.selectById(reqVo.getId());
        if (!logNestExce.getNestId().equals("领料单")) {
            throw SyExceptionUtils.e("非领料单。");
        }
        List<WmsPlatePick> platePickList = wmsPlatePickMapper.lambdaQuery().eq(WmsPlatePick::getPickingListNo, logNestExce.getConstructNo()).list();
        List<PickPlateInfoResVo> list = platePickList.stream().map(wmsPlatePick -> {
            PickPlateInfoResVo p = new PickPlateInfoResVo();
            p.setPlateCode(wmsPlatePick.getMaterialCode());
            p.setNestId(wmsPlatePick.getNestId());
            p.setPositionCode(wmsPlatePick.getStoragePlace());
            p.setLayerCode(wmsPlatePick.getSeq().toString());
            return p;
        }).collect(Collectors.toList());
        return BaseListResVo.of(list, PickPlateInfoResVo.class);
    }

    public void excePlateIn(NestPlatePushDto reqVo) {
        if (!StringUtils.hasText(reqVo.getPlateCode())) {
            throw SyExceptionUtils.e("钢板号不能为空");
        }

        String userCode = SySecurityUtils.getUserCode();
        reqVo.setPickListNo("G" + userCode);

        reqVo.setPickNo(1);

        SinoCamReceiveNestPlate plate = sinoCamReceiveNestPlateMapper.lambdaQuery().eq(SinoCamReceiveNestPlate::getStockListNo, reqVo.getPlateCode()).one();
        Result result;
        if (plate != null) {
            if (!StringUtils.hasText(reqVo.getPartId())) {
                throw SyExceptionUtils.e("零件号不能为空");
            }
            List<SinoCamReceiveNestPart> nestPartList = sinoCamReceiveNestPartMapper.lambdaQuery().eq(SinoCamReceiveNestPart::getPlateCode, reqVo.getPlateCode()).eq(SinoCamReceiveNestPart::getPartId, reqVo.getPartId()).list();
            if (nestPartList.isEmpty()) {
                result = apsMesFeign.fixNestPartInfo258(reqVo);
                if (result.getCode() == null || result.getCode() != 0) {
                    throw SyExceptionUtils.e("信息回调失败，请查看aps-mes日志。");
                }
                logNestExceMapper.lambdaUpdate().eq(LogNestExce::getPlateCode, reqVo.getPlateCode()).eq(LogNestExce::getPartId, reqVo.getPartId()).set(LogNestExce::getInfoState, 1).set(LogNestExce::getModifyDateTime, LocalDateTime.now().format(DatePattern.NORM_DATETIME_FORMATTER)).update();
            } else {
                throw SyExceptionUtils.e("信息已经回填过订单，特殊处理。");
            }
//            result = apsMesFeign.fixNestPartInfo(reqVo);
//            if (result.getCode() == null || result.getCode() != 0) {
//                throw SyExceptionUtils.e("信息回调失败，请查看aps-mes日志。");
//            }
//            logNestExceMapper.lambdaUpdate().eq(LogNestExce::getPlateCode, reqVo.getPlateCode()).eq(LogNestExce::getPartId, reqVo.getPartId()).set(LogNestExce::getInfoState, 1).update();
        } else {
            result = apsMesFeign.fixNestInfo(reqVo);
            if (result.getCode() == null || result.getCode() != 0) {
                throw SyExceptionUtils.e("信息回调失败，请查看aps-mes日志。");
            }
            logNestExceMapper.lambdaUpdate().eq(LogNestExce::getPlateCode, reqVo.getPlateCode()).set(LogNestExce::getInfoState, 1).set(LogNestExce::getModifyDateTime, LocalDateTime.now().format(DatePattern.NORM_DATETIME_FORMATTER)).update();
        }

    }

}
